'''
Created on 4. ozu. 2011.

@author: Ana Banana
'''

from igraph import Graph
from igraph.core import VertexSeq

class Network:
    
    def __init__(self, file):
        self.graph = Graph.Read_GML(file);
        print self.graph
        
    def get_graph (self):
        return self.graph;
        
    def getKdegreeNodes (self, K):
        gs = Graph.k_core(self.graph, K);
        return gs
    
    def getKcoresGraph(self, perc):
        results = {};
        gs = Graph.k_core(self.graph);
        elements = {};        
        for g in gs:
            g_perc = float(len(g.vs))/len(self.graph.vs);
            results[g_perc] = g;
            if perc-g_perc > 0 : continue;
            elements[perc-g_perc] = g_perc;
        key = sorted(elements.keys())[len(elements.keys())-1]
        return results[elements[key]]
    
    def findMinNodes (self, network):
        graph = network.get_graph();
        i = 1;
        while (True):
            k = Graph.k_core(graph, i);
            print str(k) + ', k = ' + str(i)
            if (len(k.vs) == 0 ):
                i = i-1;
                break;
            else : 
                #print 'k = ', i, ', len = ', len(k.vs)
                i += 1;
            
        return Graph.k_core(graph, i);